<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;

class CategoryController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'goods'); // 数据集
    }
    
    /*
     * 商品分类显示
     *
     */
    public function index(){
        $parent_id = I('cat_id');
        if(empty($parent_id)){
            $where['parent_id'] = 0;
        }else{
            $where['parent_id'] = $parent_id;
        }
        $arr = M("Category")->where($where)->order('sort_order asc')->select();
        foreach ($arr as $key => $value) {
            $arr[$key]['count'] = M('Category')->where('parent_id='.$value['cat_id'])->count();
        }
        $this->assign("arr", $arr);
        $this->assign('action', 'category');
        $this->display();
    }

    /*
     * 商品分类编辑
     *
     */
    public function save(){
        $tree = new \Libs\Tree\Tree();
        $category = M("Category")->select();
        $arr = $tree->channelLevel($category, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign("arr", $arr);
        $_where['pid']=0;
        $Attri = M("goodsAttri")->where($_where)->select();
        $this->assign("Attri", $Attri);
        $Para = M("goodsPara")->where($_where)->select();
        $this->assign("Para", $Para);
        $Brand = M("Brand")->where($_where)->select();
        $this->assign("Brand", $Brand);
        $where['cat_id'] = I("get.cat_id");
        if ($where) {
            $c = M("Category")->where($where)->find();
            $this->assign("c", $c);
            if (IS_POST) {
                $data = I('post.');
                $cf = M("Category")->where($where)->data($data)->save();
                $cf ? $this->success('修改成功', U('index')) : $this->error('修改失败');
            }

            $this->display();
        }
    }

    /*
     * 商品分类添加
     *
     */
    public function add(){
        $tree = new \Libs\Tree\Tree();
        $category = M("Category")->select();
        $arr = $tree->channelLevel($category, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign('arr',$arr);
        $_where['pid']=0;
        $_where['state']=1;
        $Attri = M("goodsAttri")->where($_where)->select();
        $this->assign("Attri", $Attri);
        $Para = M("goodsPara")->where($_where)->select();
        $this->assign("goodsPara", $Para);
        $Brand = M("Brand")->where($_where)->select();
        $this->assign("Brand", $Brand);
        if(IS_POST){
            $data = I('post.');
            $cf = M("Category")->data($data)->add();
            $cf ? $this->success('添加成功','index') :  $this->error('添加失败');
        }
        $this->assign('action', 'category');
        $this->display();
    }

    /*
     * 商品分类新增下级
     *
     */
    public function redact(){
        $where['cat_id'] = I("get.cat_id");
        if(isset($where)) {
            $category = M("Category");
            $parent = $category->where($where)->find();
            $this->assign('parent', $parent);

            if(IS_POST){
                $data = I('post.');
                $cf = $category->data($data)->add();
                $cf ? $this->success('操作成功',U('index')) : $this->error('操作失败');
            }
        }
        $this->assign('action', 'category');
        $this->display();
    }

    /*
     * 商品分类删除
     *
     */
    public function del(){
        $cat_id = I("post.data");
        $category = M("Category");
        $res = $category->where("parent_id = $cat_id")->select();
        if($res){
            $this->success('删除失败,请删除下级分类!');
        }else{
            $cf = $category->where(array('cat_id'=>$cat_id))->delete();
            $cf ? $this->success('删除成功') : $this->error('删除失败');
        }
    }
}